package com.cq.cqmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.common.utils.PageUtils;
import com.cq.common.utils.Query;
import com.cq.cqmall.product.dao.SkuInfoDao;
import com.cq.cqmall.product.entity.SkuImagesEntity;
import com.cq.cqmall.product.entity.SkuInfoEntity;
import com.cq.cqmall.product.entity.SpuInfoDescEntity;
import com.cq.cqmall.product.service.*;
import com.cq.cqmall.product.vo.SkuItemVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * impl sku信息服务
 *
 * @author 程崎
 * @since 2022/09/30
 */
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Resource
    private SkuImagesService skuImagesService;

    @Resource
    private SpuInfoDescService spuInfoDescService;

    @Resource
    private AttrGroupService attrGroupService;

    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> wrapper = Wrappers.lambdaQuery(SkuInfoEntity.class);
        String key = (String) params.get("key");
        String max = (String) params.get("max");
        String min = (String) params.get("min");
        String catelogId = (String) params.get("catelogId");
        String brandId = (String) params.get("brandId");

        if (StringUtils.isNotEmpty(key)) {
            wrapper.and(w -> w.eq(SkuInfoEntity::getSkuId, key)
                    .or()
                    .like(SkuInfoEntity::getSkuName, key));
        }

        if (StringUtils.isNotEmpty(min)) {
            wrapper.ge(SkuInfoEntity::getPrice, min);
        }

        if (StringUtils.isNotEmpty(max)) {
            BigDecimal bigDecimal = new BigDecimal("0");
            if (new BigDecimal(max).compareTo(bigDecimal) > 0) {
                wrapper.le(SkuInfoEntity::getPrice, max);
            }
        }

        if (StringUtils.isNotEmpty(catelogId) && !"0".equals(catelogId)) {
            wrapper.eq(SkuInfoEntity::getCatalogId, catelogId);
        }

        if (StringUtils.isNotEmpty(brandId) && !"0".equals(brandId)) {
            wrapper.eq(SkuInfoEntity::getBrandId, brandId);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return this.list(
                Wrappers.lambdaQuery(SkuInfoEntity.class)
                        .eq(SkuInfoEntity::getSpuId, spuId)
        );
    }

    @Override
    public SkuItemVO item(Long skuId) {
        SkuItemVO skuItemVO = new SkuItemVO();

        /*// 1、sku基本信息获取 pms_sku_info
        SkuInfoEntity skuInfo = this.getById(skuId);
        skuItemVO.setInfo(skuInfo);
        Long catalogId = skuInfo.getCatalogId();
        Long spuId = skuInfo.getSpuId();

        // 2、sku的图片信息
        List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
        skuItemVO.setImages(images);

        // 3、获取spu的销售属性组合
        List<SkuItemVO.SkuItemSaleAttrsVO> saleAttrsVos = skuSaleAttrValueService.getSaleAttrsBySpuId(spuId);
        skuItemVO.setSaleAttr(saleAttrsVos);

        // 4、获取spu的介绍
        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(spuId);
        skuItemVO.setDesp(spuInfoDescEntity);

        // 5、获取spu的规格参数信息
        List<SkuItemVO.SpuItemAttrGroupVO> attrVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        skuItemVO.setGroupAttrs(attrVos);*/
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            // 1、sku基本信息获取 pms_sku_info
            SkuInfoEntity skuInfo = this.getById(skuId);
            skuItemVO.setInfo(skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // 3、获取spu的销售属性组合
            List<SkuItemVO.SkuItemSaleAttrsVO> saleAttrsVos = skuSaleAttrValueService.getSaleAttrsBySpuId(skuInfo.getSpuId());
            skuItemVO.setSaleAttr(saleAttrsVos);
        }, threadPoolExecutor);

        CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // 4、获取spu的介绍
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(skuInfo.getSpuId());
            skuItemVO.setDesp(spuInfoDescEntity);
        }, threadPoolExecutor);

        CompletableFuture<Void> baseAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // 5、获取spu的规格参数信息
            List<SkuItemVO.SpuItemAttrGroupVO> attrVos = attrGroupService.getAttrGroupWithAttrsBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId());
            skuItemVO.setGroupAttrs(attrVos);
        }, threadPoolExecutor);

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            // 2、sku的图片信息
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            skuItemVO.setImages(images);
        });

        CompletableFuture.allOf(saleAttrFuture, descFuture, baseAttrFuture, imageFuture).join();
        return skuItemVO;
    }

}